package com.nondo.dean.auth.provider.impl;

import com.nondo.dean.auth.common.BusinessUtils;
import com.nondo.dean.auth.common.bean.RoleAuthorityItem;
import com.nondo.dean.auth.common.bean.RoleItem;
import com.nondo.dean.auth.common.bean.RoleMenuItem;
import com.nondo.dean.auth.domain.Role;
import com.nondo.dean.auth.domain.RoleAuthorityKey;
import com.nondo.dean.auth.domain.RoleMenuKey;
import com.nondo.dean.auth.provider.RoleManager;
import com.nondo.dean.auth.service.RoleAuthorityService;
import com.nondo.dean.auth.service.RoleMenuService;
import com.nondo.dean.auth.service.RoleService;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleManagerImpl implements RoleManager {

    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleAuthorityService roleAuthorityService;
    @Autowired
    private RoleMenuService roleMenuService;

    @Override
    public View<String> registerRole(RoleItem roleItem) {

        roleService.save(BusinessUtils.toRole(roleItem));

        if (roleItem.getId() != null) {
            return View.ofOk("角色保存成功");
        } else {
            return View.ofOk("角色修改成功");
        }

    }

    @Override
    public View<String> deleteRole(Long roleId) {

        List<RoleAuthorityKey> roleAuthorityKeys = roleAuthorityService.findByRoleId(roleId);
        if (roleAuthorityKeys != null && !roleAuthorityKeys.isEmpty()) {
            return View.ofError("当前角色已经关联了权限，请先接触关联关系");
        }
        List<RoleMenuKey> roleMenuKeyList = roleMenuService.findByRoleId(roleId);
        if (roleMenuKeyList != null && !roleMenuKeyList.isEmpty()) {
            return View.ofError("当前角色已经关联了菜单，请先接触关联关系");
        }
        roleService.delete(roleId);
        return View.ofOk("角色删除成功");
    }

    @Override
    public View<String> requestRoleAuthorityBind(RoleAuthorityItem roleAuthorityItem) {

        Role oldRole = roleService.findByRoleId(roleAuthorityItem.getRoleId());
        if (oldRole == null) {
            return View.ofError("不存在编号为：【" + roleAuthorityItem.getRoleId() + "】的角色");
        }

        updateRoleAuthority(roleAuthorityItem.getRoleId(), roleAuthorityItem.getAuthorities());

        return View.ofOk("角色权限绑定成功");
    }

    @Override
    public View<String> requestRoleMenuBind(RoleMenuItem roleMenuItem) {
        Role oldRole = roleService.findByRoleId(roleMenuItem.getRoleId());
        if (oldRole == null) {
            return View.ofError("不存在编号为：【" + roleMenuItem.getRoleId() + "】的角色");
        }

        updateRoleMenu(roleMenuItem.getRoleId(), roleMenuItem.getMenuIds());
        return View.ofOk("角色菜单关系修改成功");
    }

    @Override
    public View<Page<Role>> findRolePage(Query<RoleItem> query) {

        Query<Role> roleQuery = new Query<>();
        roleQuery.setSize(query.getSize());
        roleQuery.setPage(query.getPage());
        roleQuery.setData(BusinessUtils.toRole(query.getData()));

        return View.ofOk(roleService.findRolePage(roleQuery));
    }

    @Override
    public View<Long> findRoleByCode(String code) {

        Role role = roleService.findByCode(code);
        return View.ofOk("OK", role.getId());
    }

    /**
     * 更新角色权限信息
     *
     * @param roleId      角色ID
     * @param authorities 权限集合
     */
    private void updateRoleAuthority(Long roleId, List<Long> authorities) {

        roleAuthorityService.deleteRoleAuthority(roleId);
        for (Long authId : authorities) {
            roleAuthorityService.saveRoleAuthority(roleId, authId);
        }

    }

    /**
     * 更新角色权限信息
     *
     * @param roleId 角色ID
     * @param menus  菜单集合
     */
    private void updateRoleMenu(Long roleId, List<Long> menus) {

        roleMenuService.deleteByRoleId(roleId);
        for (Long menuId : menus) {
            roleMenuService.saveRoleMenu(roleId, menuId);
        }

    }

}
